/**
 * @author Haidar Osman
 */

var UniRelation = function(fromClass, toClass, relationObj, parentShape) {
	// console.log(relationObj.isHighlighted);
	this.fromClass = fromClass;
	this.toClass = toClass;
	this.relationObj = relationObj;
	var that = this;
	this.parentShape = parentShape;
	// this.relationObj=relationObj;
	this.group = new Kinetic.Group({

	});

	var points = [{
		x : this.fromClass.headerRect.getAbsolutePosition().x,
		y : this.fromClass.headerRect.getAbsolutePosition().y
	}, {
		x : this.toClass.headerRect.getAbsolutePosition().x,
		y : this.toClass.headerRect.getAbsolutePosition().y
	}, {
		x : this.toClass.headerRect.getAbsolutePosition().x,
		y : this.toClass.headerRect.getAbsolutePosition().y
	}, {
		x : this.toClass.headerRect.getAbsolutePosition().x,
		y : this.toClass.headerRect.getAbsolutePosition().y
	}, {
		x : this.toClass.headerRect.getAbsolutePosition().x,
		y : this.toClass.headerRect.getAbsolutePosition().y
	}, {
		x : this.toClass.headerRect.getAbsolutePosition().x,
		y : this.toClass.headerRect.getAbsolutePosition().y

	}];
	this.relationLine = new Kinetic.Polygon({
		points : points,
		//fill : styles.classHeaderRectStrokeColor,
		stroke : styles.classHeaderRectStrokeColor,
		strokeWidth : 1
	});
	this.title = new Kinetic.Text({
		x : (points[0].x + points[1].x) / 2,
		y : (points[0].y + points[1].y) / 2,
		text : that.relationObj.title,
		fontSize : 8,
		fontFamily : "Calibri",
		textFill : styles.classHeaderRectStrokeColor,
		fill : "white",
		// textStroke : "#444",
		padding : 1,
		stroke : "black",
		align : "middle",
		verticalAlign : "middle",
		draggable : true,
		visible : true
	});
	this.fromNum = new Kinetic.Text({
		x : (points[0].x + points[1].x) / 2,
		y : (points[0].y + points[1].y) / 2,
		text : that.relationObj.fromNum,
		fontSize : 6,
		fontFamily : "Calibri",
		textFill : styles.classHeaderRectStrokeColor,
		fill : "white",
		// textStroke : "#444",
		padding : 1,
		stroke : "white",
		strokeWidth : 1,
		align : "middle",
		verticalAlign : "middle",
		draggable : true,
		visible : true
	});
	this.toNum = new Kinetic.Text({
		x : (points[0].x + points[1].x) / 2,
		y : (points[0].y + points[1].y) / 2,
		text : that.relationObj.toNum,
		fontSize : 6,
		fontFamily : "Calibri",
		textFill : styles.classHeaderRectStrokeColor,
		fill : "white",
		// textStroke : "#444",
		padding : 1,
		strokeWidth : 1,
		stroke : "white",
		align : "middle",
		verticalAlign : "middle",
		draggable : true,
		visible : true
	});
	this.group.add(this.relationLine);
	this.group.add(this.title);
	this.group.add(this.fromNum);
	this.group.add(this.toNum);
	this.update = function() {
		this.currentHeight
		if(that.fromClass.classObj.name != that.toClass.classObj.name) {
			if(this.relationObj.isHighlighted) {
				that.title.stroke = styles.highlightColor;
			} else {
				that.title.stroke = "white";
				//styles.classHeaderRectStrokeColor;
			}
			if(this.relationObj.isFromNumHighlighted) {
				that.fromNum.stroke = styles.highlightColor;
			} else {
				that.fromNum.stroke = "white";
				//styles.classHeaderRectStrokeColor;
			}
			if(this.relationObj.isToNumHighlighted) {
				that.toNum.stroke = styles.highlightColor;
			} else {
				that.toNum.stroke = "white";
				//styles.classHeaderRectStrokeColor;
			}
			var X1, X2, Y1, Y2;
			var x1 = this.fromClass.headerRect.getAbsolutePosition().x;
			var x2 = this.fromClass.headerRect.getAbsolutePosition().x + this.fromClass.headerRect.width;
			var x3 = this.toClass.headerRect.getAbsolutePosition().x;
			var x4 = this.toClass.headerRect.getAbsolutePosition().x + this.toClass.headerRect.width;

			var y1 = this.fromClass.headerRect.getAbsolutePosition().y;
			var y2 = this.fromClass.headerRect.getAbsolutePosition().y + this.fromClass.currentHeight;
			var y3 = this.toClass.headerRect.getAbsolutePosition().y;
			var y4 = this.toClass.headerRect.getAbsolutePosition().y + this.toClass.currentHeight;

			if(x2 < x3) {
				X1 = x2;
				X2 = x3;
			} else if(x3 <= x2) {
				if(x2 <= x4) {
					X1 = x2;
					X2 = x2;
				} else if(x1 <= x4) {
					if(x1 <= x3) {
						X1 = x3;
						X2 = x3;
					} else {
						X1 = x1;
						X2 = x1;
					}
				} else {
					X1 = x1;
					X2 = x4;
				}
			}

			if(y2 < y3) {
				Y1 = y2;
				Y2 = y3;
			} else if(y3 <= y2) {
				if(y2 <= y4) {
					Y1 = y2;
					Y2 = y2;
				} else if(y1 <= y4) {
					Y1 = y4;
					Y2 = y4;
				} else {
					Y1 = y1;
					Y2 = y4;
				}
			}

			var headlen = 10;

			var angle = Math.atan2(Y2 - Y1, X2 - X1);

			var X3 = X2 - headlen * Math.cos(angle - Math.PI / 6);
			var Y3 = Y2 - headlen * Math.sin(angle - Math.PI / 6);
			var X4 = X2 - headlen * Math.cos(angle + Math.PI / 6);
			var Y4 = Y2 - headlen * Math.sin(angle + Math.PI / 6);

			that.relationLine.points[0].x = X1;
			that.relationLine.points[0].y = Y1;

			that.relationLine.points[1].x = X2;
			that.relationLine.points[1].y = Y2;

			that.relationLine.points[2].x = X3;
			that.relationLine.points[2].y = Y3;

			that.relationLine.points[3].x = X2;
			that.relationLine.points[3].y = Y2;

			that.relationLine.points[4].x = X4;
			that.relationLine.points[4].y = Y4;

			that.relationLine.points[5].x = X2;
			that.relationLine.points[5].y = Y2;

			that.title.x = (that.relationLine.points[0].x + that.relationLine.points[1].x) / 2;
			that.title.y = (that.relationLine.points[0].y + that.relationLine.points[1].y) / 2;

			var xShift = 1.5 * headlen * Math.abs(Math.cos(angle));
			var yShift = 1.5 * headlen * Math.abs(Math.sin(angle));
			if(X2 > X1) {
				if(Y2 > Y1) {
					that.fromNum.x = X1 + 5;
					that.fromNum.y = Y1 + yShift;
					that.toNum.x = X2 - xShift - 5;
					that.toNum.y = Y2 - yShift - 5;
				} else {
					that.fromNum.x = X1 + 5;
					that.fromNum.y = Y1 - yShift;
					that.toNum.x = X2 - xShift - 5;
					that.toNum.y = Y2 + yShift;
				}
			} else {
				if(Y2 > Y1) {
					that.fromNum.x = X1 - xShift;
					that.fromNum.y = Y1 + yShift;
					that.toNum.x = X2 + xShift;
					that.toNum.y = Y2 - yShift;
				} else {
					that.fromNum.x = X1 - xShift;
					that.fromNum.y = Y1 - yShift;
					that.toNum.x = X2 + xShift;
					that.toNum.y = Y2 + yShift;
				}
			}
		} else {
			var h = fromClass.currentHeight;
			var w = fromClass.headerRect.width;
			var x = this.fromClass.headerRect.getAbsolutePosition().x;
			var y = this.fromClass.headerRect.getAbsolutePosition().y;
			var padding = 20;
			var headlen = 10;
			var angle = Math.atan2(padding * (-1), 0);
			var X3 = x + w / 2 - headlen * Math.cos(angle - Math.PI / 6);
			var Y3 = y + h - headlen * Math.sin(angle - Math.PI / 6);
			var X4 = x + w / 2 - headlen * Math.cos(angle + Math.PI / 6);
			var Y4 = y + h - headlen * Math.sin(angle + Math.PI / 6);

			that.relationLine.points[0].x = x + w;
			that.relationLine.points[0].y = y + h / 2;

			that.relationLine.points[1].x = x + w + padding;
			that.relationLine.points[1].y = y + h / 2;

			that.relationLine.points[2].x = x + w + padding;
			that.relationLine.points[2].y = y + h + padding;

			that.relationLine.points[3].x = x + w / 2;
			that.relationLine.points[3].y = y + h + padding;

			that.relationLine.points[4].x = x + w / 2;
			that.relationLine.points[4].y = y + h;

			that.relationLine.points[5] = {
				x : X3,
				y : Y3
			}

			that.relationLine.points[6] = {
				x : x + w / 2,
				y : y + h
			}

			that.relationLine.points[7] = {
				x : X4,
				y : Y4
			}
			that.relationLine.points[8] = {
				x : x + w / 2,
				y : y + h
			}

			that.relationLine.points[9] = {
				x : x + w / 2,
				y : y + h + padding
			}

			that.relationLine.points[10] = {
				x : x + w + padding,
				y : y + h + padding
			}
			that.relationLine.points[11] = {
				x : x + w + padding,
				y : y + h / 2
			}
			that.relationLine.points[12] = {
				x : x + w,
				y : y + h / 2
			}
			that.title.x = x + w + 5;
			that.title.y = y + h + 5;
			that.fromNum.x =x + w+5;
			that.fromNum.y  = y + h / 2;			
			that.toNum.x = x+w/2;
			that.toNum.y = y+h+1.5*headlen;

		}
	}
	this.onClick = function() {
		var intent = {
			name : "highlightRelationTitle",
			arguments : [that.parentShape.parentShape.parent.name, that.fromClass.classObj.name, that.toClass.classObj.name]
		}
		that.parentShape.parentShape.parent.parent.showIntent(intent);
		var mouseX = that.title.getAbsolutePosition().x - that.title.padding + $("#" + that.title.getStage().getContainer().id).prop("offsetLeft");
		var mouseY = that.title.getAbsolutePosition().y - that.title.padding + $("#" + that.title.getStage().getContainer().id).prop("offsetTop");
		var formString = "<div id='EditTitlePopUp' style='position:absolute;top:" + mouseY + "px;left:" + mouseX + "px;z-index:30;'>" + "<input type='text' value= '" + that.relationObj.title + "' id='titleName' autofocus style='width:" + that.title.width + "px; font-size:" + that.title.fontSize + "px;  '/>" + "</div>"

		// if($('#titleName').length !== 0) {
		// $('#EditTitlePopUp').remove();
		var e = jQuery.Event("keydown");
		e.which = 27;
		that.parentShape.parentShape.parent.parent.globalEscape(e);
		// }

		$("#" + that.title.getStage().getContainer().id).before(formString);
		$('input#titleName').focus();
		$('input#titleName').select();
		$('#titleName').keydown(function(event) {
			// that.relationObj.isHighlighted=false;

			if(event.which == 13) {
				var name = $('input#titleName').val();
				// that.setName(name);
				var modelName = that.parentShape.parentShape.parent.name;
				var viewName = that.parentShape.parentShape.name;
				if(name != "") {
					var command = {
						name : "setRelationTitle",
						arguments : [modelName, viewName, that.fromClass.classObj.name, that.toClass.classObj.name, name]
					}
					that.parentShape.parentShape.parent.parent.run(command);
				} else {
					var command = {
						name : "removeRelation",
						arguments : [modelName, viewName, that.fromClass.classObj.name, that.toClass.classObj.name]
					}
					that.parentShape.parentShape.parent.parent.run(command);
				}
				$('#EditTitlePopUp').remove();
				intent = {
					name : "unHighlightRelationTitle",
					arguments : [that.parentShape.parentShape.parent.name, that.fromClass.classObj.name, that.toClass.classObj.name]
				}
				that.parentShape.parentShape.parent.parent.showIntent(intent);
			} else if(event.which == 27) {
				$('#EditTitlePopUp').remove();
				intent = {
					name : "unHighlightRelationTitle",
					arguments : [that.parentShape.parentShape.parent.name, that.fromClass.classObj.name, that.toClass.classObj.name]
				}
				that.parentShape.parentShape.parent.parent.showIntent(intent);
			}
		});
	}
	this.fromNum.on("dblclick", function() {
		var intent = {
			name : "highlightRelationFromNum",
			arguments : [that.parentShape.parentShape.parent.name, that.fromClass.classObj.name, that.toClass.classObj.name]
		}
		that.parentShape.parentShape.parent.parent.showIntent(intent);
		var mouseX = that.fromNum.getAbsolutePosition().x - that.fromNum.padding + $("#" + that.fromNum.getStage().getContainer().id).prop("offsetLeft");
		var mouseY = that.fromNum.getAbsolutePosition().y - that.fromNum.padding + $("#" + that.fromNum.getStage().getContainer().id).prop("offsetTop");
		var formString = "<div id='EditFromNumPopUp' style='position:absolute;top:" + mouseY + "px;left:" + mouseX + "px;z-index:30;'>" + "<input type='text' value= '" + that.relationObj.fromNum + "' id='fromNum' autofocus style='width:" + that.fromNum.width + "px; font-size:" + that.fromNum.fontSize + "px;  '/>" + "</div>"

		// if($('#fromNum').length !== 0) {
		// $('#EditFromNumPopUp').remove();
		var e = jQuery.Event("keydown");
		e.which = 27;
		that.parentShape.parentShape.parent.parent.globalEscape(e);
		// }

		$("#" + that.fromNum.getStage().getContainer().id).before(formString);
		$('input#fromNum').focus();
		$('input#fromNum').select();
		$('#fromNum').keydown(function(event) {
			// that.relationObj.isHighlighted=false;

			if(event.which == 13) {
				var name = $('input#fromNum').val();
				// that.setName(name);
				var modelName = that.parentShape.parentShape.parent.name;
				var viewName = that.parentShape.parentShape.name;

				var command = {
					name : "setRelationFromNum",
					arguments : [modelName, viewName, that.fromClass.classObj.name, that.toClass.classObj.name, name]
				}
				that.parentShape.parentShape.parent.parent.run(command);

				$('#EditFromNumPopUp').remove();
				intent = {
					name : "unHighlightRelationFromNum",
					arguments : [that.parentShape.parentShape.parent.name, that.fromClass.classObj.name, that.toClass.classObj.name]
				}
				that.parentShape.parentShape.parent.parent.showIntent(intent);
			} else if(event.which == 27) {
				$('#EditFromNumPopUp').remove();
				intent = {
					name : "unHighlightRelationFromNum",
					arguments : [that.parentShape.parentShape.parent.name, that.fromClass.classObj.name, that.toClass.classObj.name]
				}
				that.parentShape.parentShape.parent.parent.showIntent(intent);
			}
		});
	});

	this.toNum.on("dblclick", function() {
		var intent = {
			name : "highlightRelationToNum",
			arguments : [that.parentShape.parentShape.parent.name, that.fromClass.classObj.name, that.toClass.classObj.name]
		}
		that.parentShape.parentShape.parent.parent.showIntent(intent);
		var mouseX = that.toNum.getAbsolutePosition().x - that.toNum.padding + $("#" + that.toNum.getStage().getContainer().id).prop("offsetLeft");
		var mouseY = that.toNum.getAbsolutePosition().y - that.toNum.padding + $("#" + that.toNum.getStage().getContainer().id).prop("offsetTop");
		var formString = "<div id='EditToNumPopUp' style='position:absolute;top:" + mouseY + "px;left:" + mouseX + "px;z-index:30;'>" + "<input type='text' value= '" + that.relationObj.toNum + "' id='toNum' autofocus style='width:" + that.toNum.width + "px; font-size:" + that.toNum.fontSize + "px;  '/>" + "</div>"

		// if($('#toNum').length !== 0) {
		// $('#EditToNumPopUp').remove();
		var e = jQuery.Event("keydown");
		e.which = 27;
		that.parentShape.parentShape.parent.parent.globalEscape(e);
		// }

		$("#" + that.toNum.getStage().getContainer().id).before(formString);
		$('input#toNum').focus();
		$('input#toNum').select();
		$('#toNum').keydown(function(event) {
			// that.relationObj.isHighlighted=false;

			if(event.which == 13) {
				var name = $('input#toNum').val();
				// that.setName(name);
				var modelName = that.parentShape.parentShape.parent.name;
				var viewName = that.parentShape.parentShape.name;

				var command = {
					name : "setRelationToNum",
					arguments : [modelName, viewName, that.fromClass.classObj.name, that.toClass.classObj.name, name]
				}
				that.parentShape.parentShape.parent.parent.run(command);

				$('#EditToNumPopUp').remove();
				intent = {
					name : "unHighlightRelationToNum",
					arguments : [that.parentShape.parentShape.parent.name, that.fromClass.classObj.name, that.toClass.classObj.name]
				}
				that.parentShape.parentShape.parent.parent.showIntent(intent);
			} else if(event.which == 27) {
				$('#EditToNumPopUp').remove();
				intent = {
					name : "unHighlightRelationToNum",
					arguments : [that.parentShape.parentShape.parent.name, that.fromClass.classObj.name, that.toClass.classObj.name]
				}
				that.parentShape.parentShape.parent.parent.showIntent(intent);
			}
		});
	});

	this.isInFocus = function() {
		if(that.parentShape.parentShape.isInFocus()) {
			return true;
		}
		return false;

	}
	this.setName = function(name) {
		that.relationObj.title = name;
		that.title.text = name;
		if(that.isInFocus()) {
			that.title.getLayer().draw();
		}
	}
	this.setFromNum = function(num) {
		that.relationObj.fromNum = num;
		that.fromNum.text = num;
		if(that.isInFocus()) {
			that.title.getLayer().draw();
		}
	}
	this.setToNum = function(num) {
		that.relationObj.toNum = num;
		that.toNum.text = num;
		if(that.isInFocus()) {
			that.title.getLayer().draw();
		}
	}
	this.title.on("dblclick", this.onClick);
}